<?php

namespace Example\Controller;

use Common\Controller\AdminbaseController;

class IndexController extends AdminbaseController {

    protected $m_Example;

    function _initialize() {
        parent::_initialize();
        $this->m_Example= D("Example/Example");
    }

    /**
     * 列表页
     */
    public function index(){

        $count=$this->m_Example->count();
        $page = $this->page($count, 15);
        $list = $this->m_Example
            ->order("create_time desc")
            ->limit($page->firstRow , $page->listRows)
            ->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign("list",$list);
        $this->display();
    }


    /**
     * 新增
     */
    public function add(){
        $id = I("get.id",0,'intval');
        if(!empty($id)){
            $item=$this->m_Example->where(array('id'=>$id))->find();
            $this->assign("item",$item);
        }

        $this->display();
    }

    /**
     * 新增数据提交
     */
    public function add_post(){
        if (IS_POST) {
            $id = I("post.id");
            $insert = I("post.");
            if(empty($id)){
                $insert['create_time'] = time();
                $result =$this->m_Example->add($insert);
            }else{
                $result = $this->m_Example->where(array("id"=>$id))->save($insert);
            }
            if ($result) {
                $this->success("操作成功！");
            } else {
                $this->error("操作失败！");
            }

        }
    }



    // 删除
    public function delete(){
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            if ($this->m_Example->where(array('id'=>$id))->delete() !==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("参数有误！");
        }

    }

}